/*
 * ActivationStackPanel.java
 *
 * Created on 19 de abril de 2008, 0:25
 */

package org.perfectday.main.dummyengine.model;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.perfectday.logicengine.model.activationstack.accidents.Accident;
import org.perfectday.logicengine.model.activationstack.accidents.Activation;
import org.perfectday.logicengine.model.activationstack.accidents.OffensiveAction;
import org.perfectday.logicengine.model.minis.Mini;
import org.perfectday.logicengine.model.spells.accident.CastSpellAccident;
import org.perfectday.logicengine.model.state.accident.StateAccident;
import org.perfectday.main.dummyengine.DummyGraphicsEngine;

/**
 *
 * @author  Miguel Angel Lopez Montellano ( alakat@gmail.com )
 */
public class ActivationStackPanel extends javax.swing.JPanel {
    private static final Logger logger = Logger.getLogger(ActivationStackPanel.class);
    private final int W_EVENT = 200;
    private final int H_EVENT = 50;
    private final int INI_I = 0;
    public static final String IMG_ACTIVATION="imgac.jpeg";
    public static final String IMG_COMBAT="imgcom.jpeg";
    public static final int MAX_STRING = 6;
    public Mini selectedMini;
    private List<Accident> accident;
    private Image imgActivationMini;
    private Image imgCombatAction;
    private DummyGraphicsEngine dummyGraphicsEngine;

    public ActivationStackPanel() {
        this.accident = new ArrayList<Accident>();
        initComponents();
        this.imgActivationMini = Toolkit.getDefaultToolkit().getImage(
                CombatInformationPanel.class.getClassLoader().
                getResource("assets/"+IMG_ACTIVATION));
        this.imgCombatAction =Toolkit.getDefaultToolkit().getImage(
                CombatInformationPanel.class.getClassLoader().
                getResource("assets/"+IMG_COMBAT));
        
    }

    public void setDummyGraphicsEngine(DummyGraphicsEngine dummyGraphicsEngine) {
        this.dummyGraphicsEngine = dummyGraphicsEngine;
    }
    
            
    
    /** Creates new form ActivationStackPanel */
    public ActivationStackPanel( List<Accident> accident) {
        this.accident=accident;
        initComponents();
    }

    public synchronized      List<Accident> getAccident() {
        return accident;
    }

    public void setAccident(List<Accident> accident) {
        this.accident = accident;
    }

    public Image getImgActivationMini() {
        return imgActivationMini;
    }

    public void setImgActivationMini(Image imgActivationMini) {
        this.imgActivationMini = imgActivationMini;
    }

    public Image getImgCombatAction() {
        return imgCombatAction;
    }

    public void setImgCombatAction(Image imgCombatAction) {
        this.imgCombatAction = imgCombatAction;
    }
    
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                formMouseExited(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                formMouseMoved(evt);
            }
        });
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseMoved
        // TODO add your handling code here:
//        org.apache.log4j.Logger.getLogger(ActivationStackPanel.class).
//                info("Event("+evt.getX()+","+evt.getY()+"):"+evt.getClass().getName());
//        org.apache.log4j.Logger.getLogger(ActivationStackPanel.class).
//                info("Accident"+getSelectedAccident(evt.getX(),evt.getY()));
        Accident acc= getSelectedAccident(evt.getX(),evt.getY());
        Mini mini = null;
        if (acc != null ){           
            if( acc instanceof Activation)
                mini = ((Activation)acc).getMini();
            if (acc instanceof OffensiveAction)
                mini = ((OffensiveAction)acc).getAtacker();
            dummyGraphicsEngine.getJBattelField1().
                    setActivationStackSelectedMini(mini);
            dummyGraphicsEngine.getJBattelField1().repaint();
        }
    }//GEN-LAST:event_formMouseMoved

    private void formMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseExited
        dummyGraphicsEngine.getJBattelField1().
                setActivationStackSelectedMini(null);
    }//GEN-LAST:event_formMouseExited

    public Accident getSelectedAccident(int x,int y){
        try{
            int index = y/H_EVENT;
    //         org.apache.log4j.Logger.getLogger(ActivationStackPanel.class).
    //                debug("Index"+index);
            if(index>4)
                return null;
            if( index >= this.getAccident().size())
                return null;
            return this.getAccident().get(index);
        }catch(Exception ex){
            Logger.getLogger(ActivationStackPanel.class).error("Error de selección en evento vacío",ex);
            return null;
        }
        
    }
    @Override
    public synchronized  void paint(Graphics g) {
        logger.trace("paint");
        int i = 0;
        //Limpia lo anteriormente pintado para no solapar eventos
        g.clearRect(0, 0, 600, 600);
        if( getAccident()!=null){  
            logger.trace("Eventos : "+getAccident().size());
                for(int j =0 ;j<getAccident().size(); j++ ){
                    Accident object = getAccident().get(j);
                    paintEvent(g, i,object);    
                            i++;
                    if (i>= 5) //Las 5 proximás acciones
                        break;
                }            
        }


    }
    
    private void paintEvent(Graphics g,int i, Accident accident){

        String miniName="";
        String description="";
        Mini mini = null;     
        Image img = null;    
        g.clearRect(0,((INI_I+(i*5))+(H_EVENT*i)),W_EVENT,H_EVENT);
        if(accident instanceof Activation){
            Logger.getLogger(ActivationStackPanel.class).debug("Mini Activado Pintado:"+ ((Activation)accident).getMini().toString());
            Logger.getLogger(ActivationStackPanel.class).debug("Player mini Activado Pintado:"+ dummyGraphicsEngine.getGame().getPlayerByMini(
                    ((Activation)accident).getMini()));
            Color c = (Color) dummyGraphicsEngine.getGame().getPlayerByMini(
                    ((Activation)accident).getMini()).getDitingibleBandObject();
            g.setColor(c);        
            g.drawRect(0,((INI_I+(i*5))+(H_EVENT*i)),W_EVENT,H_EVENT);   
            miniName = ((Activation)accident).getMini().getName();
            mini = ((Activation)accident).getMini();
            description = 
                    ((Activation)accident).getMini().getMiniLevel().toString();
            description = description.substring(0,3);
            description += "/"+ ((Activation)accident).getMini().getMiniType().toString().substring(0, 3);
            description +=" ..."+((Activation)accident).getUnitTime().toString();
                   
            if ( miniName.length()>MAX_STRING)
                miniName = miniName.substring(0,MAX_STRING);            
            img = this.imgActivationMini;
        }else if ( accident instanceof OffensiveAction){
            Color c = (Color) dummyGraphicsEngine.getGame().getPlayerByMini(
                    ((OffensiveAction)accident).getAtacker()).getDitingibleBandObject();
            g.setColor(c);        
            g.drawRect(0,((INI_I+(i*5))+(H_EVENT*i)),W_EVENT,H_EVENT); 
            mini=((OffensiveAction) accident).getAtacker();
            miniName = ((OffensiveAction) accident).getAtacker().getName();
            if (miniName.length() > MAX_STRING )
                miniName = miniName.substring(0,MAX_STRING);
            description = ((OffensiveAction) accident).getInstanceCombat().getAtack().getName();
            if ( description.length()>MAX_STRING)
                description = description.substring(0,MAX_STRING);
            img = this.imgCombatAction;
        } else if (accident instanceof StateAccident){
            //TODO modificar en el futuro
            Color c = (Color) dummyGraphicsEngine.getGame().getPlayerByMini(
                    ((StateAccident)accident).getState().getMini()).getDitingibleBandObject();
            g.setColor(c);        
            g.drawRect(0,((INI_I+(i*5))+(H_EVENT*i)),W_EVENT,H_EVENT); 
            mini=((StateAccident)accident).getState().getMini();
            miniName = ((StateAccident)accident).getState().getMini().getName();
            if (miniName.length() > MAX_STRING )
                miniName = miniName.substring(0,MAX_STRING);
            description = ((StateAccident) accident).getState().getName();
            if ( description.length()>MAX_STRING)
                description = description.substring(0,MAX_STRING);
            img = this.imgCombatAction;
        }else if(accident instanceof CastSpellAccident){
             Color c = (Color) dummyGraphicsEngine.getGame().getPlayerByMini(
                    ((CastSpellAccident)accident).getInstanceCastSpell().getCaster()).getDitingibleBandObject();
            g.setColor(c);        
            g.drawRect(0,((INI_I+(i*5))+(H_EVENT*i)),W_EVENT,H_EVENT); 
            mini=((CastSpellAccident)accident).getInstanceCastSpell().getCaster();
            miniName = ((CastSpellAccident)accident).getInstanceCastSpell().getCaster().getName();
            if (miniName.length() > MAX_STRING )
                miniName = miniName.substring(0,MAX_STRING);
            description = ((CastSpellAccident)accident).getInstanceCastSpell().getSpellcast().getName();
            if ( description.length()>MAX_STRING)
                description = description.substring(0,MAX_STRING);
            img = this.imgCombatAction;
        }
        
        g.setColor(Color.black);
        
        if(this.selectedMini !=null &&
                this.selectedMini.equals(mini)){
            org.apache.log4j.Logger.getLogger(ActivationStackPanel.class).debug(
                "¿"+this.selectedMini.getName()+"="+mini.getName()+"?");
            org.apache.log4j.Logger.getLogger(ActivationStackPanel.class).debug(
                "if :"+this.selectedMini !=null &&
                this.selectedMini.equals(mini));
            Color c0 = g.getColor();
            g.setColor(Color.red);
            g.setFont(new Font(Font.MONOSPACED, Font.BOLD, 12));
            g.drawRect(0+10-1,((INI_I+(i*5))+(H_EVENT*i)+10)-1, 41, 32);
            g.setColor(c0);
            org.apache.log4j.Logger.getLogger(ActivationStackPanel.class).info("Font: BLOD");
        }else{
            g.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
//            org.apache.log4j.Logger.getLogger(ActivationStackPanel.class).info("Font: PLAIN");
        }
//        org.apache.log4j.Logger.getLogger(ActivationStackPanel.class).info(miniName);
        g.drawImage(img, 0+10,(INI_I+(i*5))+(H_EVENT*i)+10, this);
        
        g.drawString(miniName, 60,((INI_I+(i*5))+(H_EVENT*i))+20);
        g.drawString(description, 60,((INI_I+(i*5))+(H_EVENT*i))+40);
                    
    }

    public Mini getSelectedMini() {
        return selectedMini;
    }

    public void setSelectedMini(Mini selectedMini) {
        this.selectedMini = selectedMini;
        this.repaint();
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    
}
